package com.hsq.ndmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.hsq.ndmall.mq.constant.OrderRabbitMQ.*;

/**
 * @author 黄四清
 * @version 1.0.0
 * @ClassName OrderMqConfig.java
 * @Description TODO  订单配置服务
 * @createTime 2021年09月06日 19:30:00
 */
@Configuration
@ConditionalOnProperty(name = "spring.order.mq.enabled", matchIfMissing = false, havingValue = "true")
public class OrderMqConfig {
    /*TODO TTL消息 定时*/
    public static class OrderEventConfig {
        /*TODO 1.1个交换机*/
        @Bean
        public TopicExchange orderEventExchange() {
            return new TopicExchange(ORDER_EVENT_EXCHANGE, true, false);
        }
        /*TODO 2.2个队列*/

        /*2.1.普通对队列*/
        @Bean
        public Queue orderCloseQueue() {
            return new Queue(ORDER_CLOSE_QUEUE, true, false, false);
        }

        /*2.1.TTL队列*/
        @Bean
        public Queue orderCreateQueue() {
            return QueueBuilder.durable(ORDER_CREATE_QUEUE)
                    .exclusive()
                    .ttl(120000) //1分钟
                    .deadLetterExchange(ORDER_EVENT_EXCHANGE)
                    .deadLetterRoutingKey(ORDER_CLOSE_MSG)
                    .build();

        }

        @Bean
        public Queue couponReleaseQueue(){
            return new Queue(ORDER_RELEASE_COUPON_QUEUE,true,false,false);
        }

        /*TODO 3.2个队列绑定到交换机*/
        @Bean
        public Binding orderCreateQueueBinding() {
            return BindingBuilder.bind(orderCreateQueue()).to(orderEventExchange()).with(ORDER_CREATE_BINDINGKEY);
        }

        @Bean
        public Binding orderCloseBinding() {
            return BindingBuilder.bind(orderCloseQueue()).to(orderEventExchange()).with(ORDER_CLOSE_BINDINGKEY);
        }

        // 将释放优惠券
        @Bean
        public Binding couponReleaseBinding() {
            return BindingBuilder.bind(couponReleaseQueue()).to(orderEventExchange()).with(ORDER_RELEASE_OTHER_BINDINGKEY);
        }
    }
}
